<template>
    <!-- homeSub/pages/messageCenter/messageCenter.wxml -->
    <view :style="objColor" class="message-wrap" v-if="loading">
        <view class="message-box" v-if="messageList.length">
            <view class="message-list">
                <view
                    class="message-list-item"
                    :data-type="item.type"
                    :data-relationId="item.relationId"
                    :data-id="item.id"
                    :data-index="index"
                    @tap="naviTo"
                    v-for="(item, index) in messageList"
                    :key="index"
                >
                    <view class="message-list-header">
                        <view class="message-list-header-left">
                            <image src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/system.png" alt="" class="message-list-icon" v-if="item.type == 0" />
                            <image src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/notices.png" alt="" class="message-list-icon" v-if="item.type == 1" />
                            <image src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/Coupons.png" alt="" class="message-list-icon" v-if="item.type == 2" />
                            <image src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/feedback.png" alt="" class="message-list-icon" v-if="item.type == 3" />
                            <text class="icon-badge" v-if="item.isRead == 0"></text>
                            <text class="message-list-title">{{ item.title }}</text>
                        </view>
                        <view class="message-list-header-right">{{ item.releaseTime }}</view>
                    </view>

                    <view class="message-list-content">{{ item.content }}</view>
                </view>
            </view>
            <!-- <view class="loading" wx:if="{{loading}}"><van-loading type="spinner" size="40rpx"/></view> -->
            <view class="read" @tap.stop.prevent="read" v-if="messageList.length && readStatus">
                <image src="https://oos-cn.ctyunapi.cn/front-end/static/homeSub/images/Read.png" class="read-icon"></image>
                <view class="read-text">一键已读</view>
            </view>
        </view>
        <view v-else>
            <Upgrading text="暂无内容"></Upgrading>
        </view>
    </view>
</template>

<script>
'use strict';
import Upgrading from '../../../components/upgrading/upgrading';
// import vanLoading from '@vant/weapp/loading/index';

// exports.__esModule = true; // homeSub/pages/messageCenter/messageCenter.ts

var https_1 = require('./../../../api/https.js');
var util_1 = require('./../../../utils/util.js');
var app = getApp();
export default {
    components: {
        Upgrading,
    },
    data() {
        return {
            marketingSwitch:app.globalData.marketingSwitch,
			color: '#000',
            messageList: [],
            page: 1,
            pageNum: 0,
            readStatus: true,
            loading: false,
            // 订单状态
            orderStatus: [
                {
                    state: '1',
                    val: 1
                },
                {
                    state: '2',
                    val: 1
                },
                {
                    state: '3',
                    val: 1
                },
                {
                    state: '4',
                    val: 1
                },
                {
                    state: '5',
                    val: 1
                },
                {
                    state: '6',
                    val: 1
                },
                {
                    state: '7',
                    val: 1
                },
                {
                    state: '8',
                    val: 1
                },
                {
                    state: '9',
                    val: 1
                },
                {
                    state: '10',
                    val: 2
                },
                {
                    state: '11',
                    val: 2
                },
                {
                    state: '12',
                    val: 2
                },
                {
                    state: '13',
                    val: 2
                },
                {
                    state: '14',
                    val: 3
                },
                {
                    state: '15',
                    val: 2
                },
                {
                    state: '16',
                    val: 4
                },
                {
                    state: '17',
                    val: 3
                },
                {
                    state: '18',
                    val: 4
                }
            ]
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function () {
        if(uni.getStorageSync('token')) {
            this.getMessagePage(2);
            this.getIsRead();
        }else {
            this.loading = true
        }
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var page = this.page + 1; //获取当前页数并+1

        if (this.page < this.pageNum) {
            this.page=page
            this.getMessagePage(2);
        }
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.page = 1;

        if (this.messageList.length) {
            uni.showNavigationBarLoading(); //在标题栏中显示加载图标

            this.getMessagePage(1);
        }
    },
    methods: {
        naviTo: function (e) {
            var index = e.currentTarget.dataset.index;
            var type = e.currentTarget.dataset.type;
            var relationid = e.currentTarget.dataset.relationid;
            var id = e.currentTarget.dataset.id;
            this.page=1

            switch (type) {
                case 0:
                    this.check(relationid, id); // this.singleRead(id)

                    break;

                case 1:
                    this.singleRead(id);
					uni.navigateTo({
					    url: '../../pages/platformAnnouncement/platformAnnouncement?id=' + relationid
					});
                    break;

                case 2:
                    this.singleRead(id);
					
                    uni.navigateTo({
                        url: '../../../mine/pages/cardVoucher/cardVoucher'
                    });
                    break;

                case 3:
                    this.singleRead(id);
                    uni.navigateTo({
                        url: '../../pages/feedbackDetails/feedbackDetails?id=' + relationid
                    });
                    break;
            } // this.getIsRead()
        },

        // 判断订单状态
        judgmentState: function (e) {
            var type = '';
            this.orderStatus.map(function (item) {
                if (e == item.state) {
                    type = item.val;
                }
            });
            return type;
        },

        // 校验是否在充电和未支付订单
        check: function (relationid, id) {
            var that = this;
            var tmp = this;
            tmp.singleRead(id);
            https_1
                .myBillDetails({
                    id: relationid
                })
                .then(function (res) {
                    if (res.code == 0) {
                        //item.type==1?'充电结束':item.type==2?'去支付
                        var statu = Number(that.judgmentState(res.data.billStatus));

                        if (statu == 1) {
                            uni.navigateTo({
                                url: '/echartsPages/pages/power/power?busId=' + res.data.busId + '&pileNo=' + res.data.pileNumber + '&gunNo=' + res.data.gunNumber
                            });
                        } else {
                            if(!that.marketingSwitch) {
						uni.navigateTo({
							url: '/orderSub/pages/orderSettlement/orderSettlement?busId=' + res.data.busId
						});
					} else {
						uni.navigateTo({
							url: '/orderSub/pages/orderSettlementYxhk/orderSettlement?busId=' + res.data.busId
						});
					}
							// uni.navigateTo({
							//     url: '/orderSub/pages/orderSettlement/orderSettlement?busId=' + res.data.busId
							// });
                        }

                    }
                }); // if(!i){
            //     myBillPage({
            //         billStatusList: [11,13],
            //         current: 1,
            //         endTime: null,
            //         memberType: "null",
            //         size: 10,
            //         startTime: null,
            //     }).then((res:any)=>{
            //         if(res.code ==0){
            //           if(res.data.records.length){
            //             wx.navigateTo({
            //                 url:`../../../orderSub/pages/orderSettlement/orderSettlement?busId=${res.data.records[0].busId}`
            //               })
            //             //   tmp.singleRead(id)
            //           }else{
            //             querMemberStatus().then((res:any)=>{
            //                 if(res.code== 0){
            //                     if (res.data.ifCharging) {
            //                        wx.navigateTo({
            //                             url: `../../pages/power/power?busId=${res.data.billList[0].busId}&pileNo=${res.data.billList[0].pileNo}&gunNo=${res.data.billList[0].gunNo}`
            //                         })
            //                         // tmp.singleRead(id)
            //                     }
            //                 }
            //             })
            //           }
            //         }
            //       })
            // }
        },

        getMessagePage: function (index) {
            var that = this;
            https_1
                .messagePage({
                    current: that.page,
                    pageSize: 10
                })
                .then(function (res) {
                    if (res.code == 0) {
                        if (res.code == 0) {
                            var total = res.data.total;
                            var pageNum = Math.ceil(total / 10);
                            var messageList = void 0;

                            switch (index) {
                                // 下拉刷新
                                case 1:
                                    messageList = res.data.records;
                                    uni.hideNavigationBarLoading(); //在标题栏中隐藏加载图标

                                    uni.stopPullDownRefresh();
                                    break;
                                // 上拉加载或初始化页面

                                case 2:
                                    messageList = that.messageList.concat(res.data.records);
                                    break;
                            }

                            that.messageList=messageList,
                                that.pageNum=pageNum
                        }
                    }
                });
        },

        read: function () {
            var that = this;
            https_1.messageRead({}).then(function (res) {
                if (res.code == 0) {
                    that.page=1
                    that.getIsRead();
                }
            });
        },

        singleRead: function (id) {
            var that = this;
            https_1
                .messageRead({
                    messageId: id
                })
                .then(function (res) {
                    if (res.code == 0) {
                        that.getIsRead();
                    }
                });
        },

        getIsRead: function () {
            var that = this;
            uni.showLoading({
                title: '加载中'
            });
            var tmp = this;
            https_1
                .isRread({})
                .then(function (res) {
                    if (res.code == 0) {
                        tmp.readStatus=res.data,
                            tmp.page=1,
                            tmp.loading=true
                        that.getMessagePage(1);
                    }

                    uni.hideLoading();
                })
                ['catch'](function (err) {
                    console.log(err);
                });
        }
    }
};
</script>
<style lang="less" scoped>
/* homeSub/pages/messageCenter/messageCenter.wxss */
@import "../../../commin/commin.less";
page{
    background-color: @text-color6;
}
.message-wrap {
	background-color: @text-color6;
	min-height: 100vh;
}
.message-box {
	padding: 32rpx;
}
.message-list{
    background: linear-gradient(0deg, #FFFFFF, #F4F6F8);
    .borders(2rpx,@white-color);
    box-shadow: 4rpx 7rpx 20rpx 0rpx rgba(166, 171, 179, 0.24);
    border-radius: 16rpx;
    z-index: 9;
    .message-list-item{
        border-bottom: 1rpx solid @text-color5;
        font-family: PingFang SC;
        .margins(auto,32rpx,auto,32rpx);
        .message-list-header{
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-top: 32rpx;
            .message-list-header-left{
                display: flex;
                justify-content: start;
                align-items: center;
                position: relative;
                .message-list-icon{
                    .kuangao(48rpx,48rpx);
                }
                .icon-badge{
                    .position(absolute,-14rpx,auto,auto,48rpx);
                    .kuangao(14rpx,14rpx);
                    background: @orange;
                    border-radius: 7rpx;
                }
                .message-list-title{
                    margin-left:16rpx;
                    .kuan(310rpx);
                    .yiman(1);
                    .fontsize(30rpx);
                    font-weight: bold;
                    color: @text-color1;
                }
            }
            .message-list-header-right{
                .fontsize(24rpx);message-list-content
                font-weight: 400;
                color: @text-color3;
            }
        }
        .message-list-content{
            .margins(15rpx,auto,32rpx,64rpx);
            .kuan(565rpx);
            .fontsize(28rpx);
            font-weight: 400;
            color: #18181A;
            .yiman(1);
        }
    }
    .message-list-item:nth-last-child(1){
        border-bottom:none;
    }
}
.loading{
    .textalign();
}
.read{
    z-index: 99;
    .position(fixed,auto,32rpx,32rpx,auto);
    .flexs();
    .kuangao(120rpx,120rpx);
	background: var(--btnColor);
    // background: linear-gradient(0deg, #1487FA, #81C0FF);
    box-shadow: 0rpx 8rpx 8rpx 1rpx rgba(166, 171, 179, 0.29);
    border-radius: 50%;
    .fontsize(22rpx);
    font-family: PingFang SC;
    font-weight: 500;
    color: @white-color;
    .read-icon{
        .kuangao(39rpx,45rpx);
    }
    .read-text{
        margin-top: 9rpx;
    }
}
</style>
